<?php
/**
 * OpenID Functions
 *
 * @package Extensions
 * @version @@VERSION@@
 * @subpackage @@EXTENSIONNAME@@
 * @copyright Damien Lebrun 2007
 * @author Damien Lebrun
 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GPLv2
 */

/**
 * Add OpenId settings to conf/settings.php.
 * @param Context $Context Vanilla context object
 * @return void
 */
function OpenID_Setup(&$Context, $VersionRef, $OpenIdDir) {
	if (!array_key_exists('OPENID_VERSION', $Context->Configuration)
		|| version_compare($Context->Configuration['OPENID_VERSION'], $VersionRef, '<')
	) {
		// Add configuration settings
		$OpenIdDir = str_replace('\\', '/',$OpenIdDir);
		$Config = array(
			'OPENID_ROOT'		=> 'extensions/OpenID/',
			'OPENID_CACHE_DIR'	=> $OpenIdDir . 'cache/',
			'OPENID_PATH'		=> $OpenIdDir
		);
		foreach ($Config as $key => $value) {
			if (!array_key_exists($key, $Context->Configuration)) {
				AddConfigurationSetting($Context, $key, $value);
			}
			$Context->Configuration[$key] = $value;
		}

		// Add DB settings
		if (!array_key_exists('OpenID_OpenId', $Context->DatabaseTables)) {
			AppendToConfigurationFile(
					$Context->Configuration['DATABASE_PATH'],
						"\$DatabaseTables['OpenID_OpenId'] = 'OpenID_OpenId';\n"
						."\$DatabaseColumns['OpenID_OpenId']['Url'] = 'Url';\n"
						."\$DatabaseColumns['OpenID_OpenId']['UserID'] = 'UserID';\n"
						."\$DatabaseColumns['User']['OpenId_UseOpenId'] = 'OpenId_UseOpenId';\n"
			);
			$Context->DatabaseTables['OpenID_OpenId'] = 'OpenID_OpenId';
			$Context->DatabaseColumns['OpenID']['Url'] = 'Url';
			$Context->DatabaseColumns['OpenID']['UserID'] = 'UserID';
			$Context->DatabaseColumns['User']['OpenId_UseOpenId'] = 'OpenId_UseOpenId';
		}

		AddConfigurationSetting($Context, 'OPENID_VERSION', $VersionRef);
	}
	OpenID_InstallDB($Context);
}

/**
 * Add the tables and field require for OpeID
 *
 * @param Context $Context
 * @return void
 */
function OpenID_InstallDB(&$Context) {
	if (!array_key_exists('OPENID_DB_INSTALLED', $Context->Configuration)
		|| !$Context->Configuration['OPENID_DB_INSTALLED']
	) {
		$senderObj = 'OpenID (Extension)';
		$senderMethod = 'OpenID_InstallDB';

		$CreateTable = 'CREATE TABLE IF NOT EXISTS `'
			. $Context->Configuration['DATABASE_TABLE_PREFIX']
			. 'OpenID_OpenId` ('
			.'  `Url` VARCHAR( 255 ) NOT NULL ,'
			.'  `UserID` INT NOT NULL ,'
			.'  PRIMARY KEY ( `Url` ) ,'
			.'  INDEX ( `UserID` )'
			.');';
		$Context->Database->Execute(
			$CreateTable, $senderObj, $senderMethod,
			'Could not create the OpenID Table');


		$ShowUserFields = 'show columns from '.$Context->DatabaseTables['User'];
		$DataSet = $Context->Database->Execute($ShowUserFields, $senderObj, $senderMethod, 'Could not Find');
		$UserColumn = array();
		while ($row = $Context->Database->GetRow($DataSet)) {
			$UserColumn[] = $row['Field'];
		}
		if (!in_array('OpenId_UseOpenId', $UserColumn)) {
			$AlterTable = 'ALTER TABLE `'
				.$Context->DatabaseTables['User']
				.'` ADD `OpenId_UseOpenId` BOOL NOT NULL DEFAULT \'0\';';
			$Context->Database->Execute(
				$AlterTable, $senderObj, $senderMethod,
				'Could not add OpenId_UseOpenId to ' . $Context->DatabaseTables['User']);
		}

		if ($Context->ErrorManager->Iif()) {
			AddConfigurationSetting($Context, 'OPENID_DB_INSTALLED', '1');
		}
	}
}

function OpenID_ThemeFilePath(&$Configuration, $FileName) {
	if (file_exists($Configuration['THEME_PATH'].$FileName)) {
		return $Configuration['THEME_PATH'].$FileName;
	} else {
		return $Configuration["OPENID_PATH"]."themes/".$FileName;
	}
}